<?php 

session_start();
# auto-login as 'default' user or guest account
require 'conf.php';
require 'login.php';


function haspic($session, $username)
{ 
    global $MW_host;
    $fields = array('USR_HAS_PIC' => '',
                    'SESSION' => $session,
                    'USERNAME' => $username);

    $fields = http_build_query($fields);

    $h = curl_init($MW_host); 
    curl_setopt($h, CURLOPT_POST, 1);
    curl_setopt($h, CURLOPT_POSTFIELDS, $fields); 
    curl_setopt($h, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($h, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($fields))); 

    $res = curl_exec($h);
    if(!mb_check_encoding($res, 'UTF-8'))
        $res = utf8_encode($res);
    $data = json_decode($res);
    return $data;
}

$data = haspic($_SESSION['sessiontoken'], $_SESSION['username']);
$haspic = $data != null && isset($data->haspic) && $data->haspic == true ? true : false;
  
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Kalender</title>
<link rel='stylesheet' type='text/css' href='css/kalender.css'>
<link rel='stylesheet' type='text/css' href='fullcalendar/fullcalendar.css'>
<link rel='stylesheet' type='text/css' href='fullcalendar/fullcalendar.print.css' media='print'>
<link rel='stylesheet' type='text/css' href='css/ui-lightness/jquery-ui-1.8.13.custom.css'>
<script type='text/javascript' src='js/jquery/jquery-1.5.2.min.js'></script>
<script type='text/javascript' src='js/jquery/jquery-ui-1.8.13.custom.min.js'></script>
<script type='text/javascript' src='fullcalendar/fullcalendar.js'></script>
<script type='text/javascript' src="js/kalender.js"></script>
<script>
    var isUser = <?= strcmp($_SESSION['username'], 'default') ?>;
</script>
</head>
<body>

<h1>Kalender</h1>

<div id="welcomemsg">
    <p><?= $_SESSION['greet'] ?></p>
<? if ($_SESSION['username'] == 'default'): ?>
        <a href="#" onclick="$('#login_window').dialog('open')">Logga in</a><br>
        <a href="#" onclick="$('#create_user_window').dialog('open')">Skapa användare</a>
<? else: ?>
    <form id="logout_form" method="post" action="logout.php">
        <a href="#" onclick="$('#logout_form').submit()">Logga ut</a>
    </form>
    <a href="#" onclick="$('#change_password_window').dialog('open')">Ändra lösenord</a><br>
    <a href="#" onclick="$('#change_picture_window').dialog('open')">Ändra profilbild</a><br>
<? endif; ?>

<? if ($_SESSION['username'] == 'admin'): ?>
    <a href="admin.php">Administrativa funktioner</a>
<? endif; ?>

</div>

<? if ($haspic): ?>
<img src="image.php?username=<?= $_SESSION['username']?>" width=100 height=100>
<? endif; ?>

<div id='calendar'></div>

<div id="login_window" title="Inloggning">
    <form id="login_form" action="/login.php" method="post">
        <p>Användarnamn:</p>
        <input type="text" name="username">
        <p>Lösenord:</p>
        <input type="password" name="password">
    </form>
</div>

<div id="change_password_window" title="Ändra lösenord">
    <form id="change_password_form" action="/change_password.php" method="post">
        <p>Gammalt lösenord:</p>
        <input type="password" name="old_password">
        <p>Nytt lösenord:</p>
        <input type="password" name="new_password">
        <p>Nytt lösenord igen:</p>
        <input type="password" name="new_password2">
        <div id="change_password_msg" style="display: none; padding-top: 10px"></div>
    </form>
</div>

<div id="create_user_window" title="Skapa användare">
    <form id="create_user_form" action="create_user.php" method="post">
        <p>Användarnamn:</p>
        <input type="text" name="username">
        <p>Namn:</p>
        <input type="text" name="name">
        <p>Lösenord:</p>
        <input type="password" name="password">
        <p>Lösenord igen:</p>
        <input type="password" name="password2">
        <div id="create_user_msg" style="display: none; padding-top: 10px"></div>
    </form>
</div>


<div id="change_picture_window" title="Ändra profilbild">
    <form id="change_picture_form" enctype="multipart/form-data" action="change_picture.php" method="POST">
        <? 
            $data = haspic($_SESSION['sessiontoken'], $_SESSION['username']);
            $haspic = $data->haspic == true ? true : false;
            if ($haspic): ?>
                <p>Nuvarande profilbild:</p>
                <img src="image.php?username=<?= $_SESSION['username'] ?>" width=200 height=200>
            <? else: ?>
                <p>Du har ingen profilbild</p>
            <? endif; ?>
        <br>
        Välj en jpeg-bild att ladda upp: <input name="uploadedfile" type="file"><br>
    </form>
</div>

<div id="event_details_window" title="Beskrivning av händelse">
    Id: <span id="event_id"></span><br>
    Beskrivning: <span id="event_desc"></span><br>
    Starttid: <span id="event_start"></span><br>
    Stopptid: <span id="event_stop"></span><br>
    Publik händelse: <span id="event_pubshare"></span><br>
    Ägare: <span id="event_name"></span><br>
    Händelsetyp: <span id="event_etype"></span><br>
    <div id="event_details_msg" style="display: none; padding-top: 10px"></div>
</div>

<div id="add_event_window" title="Lägg till händelse">
    <form id="add_event_form" action="add_event.php" method="post">
        <p>Beskrivning:</p>
        <input id="add_event_desc" type="text" name="e_desc">
        <p>Start:</p>
        <input id="add_event_start" type="hidden" name="start">
        <p id="add_event_start_human"></p>
        <p>Stopp:</p>
        <input id="add_event_end" type="hidden" name="end">
        <p id="add_event_end_human"></p>
        <p>Publik händelse: 
        <input id="add_event_pubshare" type="checkbox" name="pubshare" value="true">
        </p>
        <p>Händelsetyp:</p>
        <p id="etype-radio"></p>
        <p id="etype-custom" style="display: none"><label for="etype-custom">Egen eventtyp</label><br><input type="text" name="etype-custom"></p>
        <div id="add_event_msg" style="display: none; padding-top: 10px"></div>
    </form>
</div>

</body>
</html>
